//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_setGear.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);

/*_gear =
[
	_uniform,0
	_vest,1
	_goggles,2
	_headgear,3
	_back_pack,4
	_back_pack_items,5
	_back_pack_weap,6
	_weapons,7
	_prim_items,8
	_sec_items,9
	_handgun_items,10
	_items_assigned,11
	_uniform_items,12
	_uniform_weap,13
	_vest_items,14
	_vest_weap,15
	_weap_sel,16
	_fire_mode,17
	_ammo 18
];*/

private ["_unit","_gear","_id","_x","_ammo"];
_unit = _this select 0;
_gear = _this select 1;
_id = 0;
#ifdef __FORCE_TRACE__
{ TRACE_1("",_x);
} foreach _gear;
#endif

removeAllweapons _unit;
removeuniform _unit;
removevest _unit;
removeheadgear _unit;
removegoggles _unit;
removeBackPack _unit;
{_unit removeItem _x} foreach (items _unit);
{_unit unassignItem _x;_unit removeItem _x} foreach (assignedItems _unit);
////////////////////////
if ((_gear select 0) != "") then {_unit addUniform (_gear select 0);};
if ((_gear select 1) != "") then {_unit addVest (_gear select 1);};
_unit addBackpack "B_AssaultPack_blk";

if (count (_gear select 11) > 0) then {{if ([_x] call FUNC(isBinocular)) then {_unit addWeapon _x;};if (_x != "" && !([_x] call FUNC(isBinocular))) then {_unit addItem _x;_unit assignItem _x;sleep 0.01;};} foreach (_gear select 11);};
removeBackPack _unit;
if ((_gear select 4) != "") then {_unit addBackPack (_gear select 4);clearAllItemsFromBackpack _unit;};

_ammo = _gear select 18;
if (count (_ammo select 0) > 0) then {
	if (typename ((_ammo select 0) select 0) == typename []) then {
		{ _unit addMagazine _x } foreach (_ammo select 0);
	} else {
		_unit addMagazine (_ammo select 0);
	};
};
if (count (_ammo select 1) > 0) then {_unit addMagazine (_ammo select 1)};
if (count (_ammo select 2) > 0) then {_unit addMagazine (_ammo select 2)};
{if (isClass (configFile >> "cfgWeapons" >> _x)) then {_unit addweapon _x;};} foreach (_gear select 7);
{
	if (!(isClass (configFile >> "cfgMagazines" >> _x))) then {
		_unit addItemToUniform _x; 
	} else {
		_unit addMagazine _x;
	};
	sleep 0.01;
} foreach (_gear select 12);
{
	if (!(isClass (configFile >> "cfgMagazines" >> _x))) then {
		_unit addItemToVest _x; 
	} else {
		_unit addMagazine _x;
	};
	sleep 0.01;
} foreach (_gear select 14);
{
	if (!(isClass (configFile >> "cfgMagazines" >> _x))) then {
		_unit addItemToBackpack _x; 
	} else {
		_unit addMagazine _x;
	};
	sleep 0.01;
} foreach (_gear select 5);

{_unit addMagazine _x;} foreach (_ammo select 3);
{_unit addMagazine _x;} foreach (_ammo select 4);
{_unit addMagazine _x;} foreach (_ammo select 5);

if ((_gear select 2) != "") then {_unit addGoggles (_gear select 2);};
if ((_gear select 3) != "") then {_unit addHeadgear (_gear select 3);};
// if (count ((_gear select 6) select 0) > 0) then 
// {
	// for "_i" from 0 to (count ((_gear select 6) select 0) - 1) do
	// {
		// (unitBackpack _unit) addweaponCargoGlobal [((_gear select 6) select 0) select _i,((_gear select 6) select 1) select _i];
	// };			
// };
removeAllPrimaryWeaponItems _unit;
if (count (_gear select 8) > 0) then {{if (_x != "") then {_unit addPrimaryWeaponItem _x;};} foreach (_gear select 8);};
if (count (_gear select 9) > 0) then {{if (_x != "") then {_unit addSecondaryWeaponItem _x;};} foreach (_gear select 9);};
if (count (_gear select 10) > 0) then {{if (_x != "") then {_unit addHandgunItem _x;};} foreach (_gear select 10);};
_unit selectweapon (_gear select 16);
if ((_gear select 17) != -1) then {player action ["SWITCHWEAPON", player, player, (_gear select 17)];};
